#ifndef RF_H_
#define RF_H_






#define RF1 0
#define RF2 1


#define BIT_RF1 (1)
#define BIT_RF2 (1<<1)


#define JUMP_CH_COUNT    					30        //频道总数
//#define DEV_ID         						20201001
#define FRAME_SLOT_COUNT        			64   				//64个slot表示一帧
#define RF_NO_RX_REBOOT_TICKS   			(100*120)			//120秒

#define RF_SEND_ERROR_REBOOT_COUNT   		10

#define SENSOR_MAX_COUNT 					128

#define PACKET_SEQ_MAX                      120

#define SENSOR_SLOT_BEGIN                   6        //SENSOR 时间槽起始
#define SENSOR_SLOT_END                    59        //SENSOR 时间槽结束



// rf状态记录表
typedef struct _rf_status_manage
{
	int8_t rf_init_ok;
	uint32_t reboot_count;
	int8_t rf_work_status;         //rf 所处的tx rx sb 等操作模式
	uint32_t rf_send_count;        //发送的包数
	uint32_t rf_send_ok_count;	   //发送ok的包数
	volatile uint32_t rf_last_rx_tick;  //最后一条收到数据的tick点
	int8_t rssi;
	int32_t rssi_add;
	int32_t rssi_count;
}struct_rf_status_manage;


#pragma anon_unions
#pragma pack(1)

/*****************AP-sensor 通信协议****************************/


typedef struct _rf_head
{
	uint16_t crc;
	uint16_t dev_id;
	struct 
	{
		uint8_t packet_type:4;
		#define RF1_AP_SYN          1
		#define RF1_AP_UPDATA       2
		#define RF1_AP_ACK          3

		#define RF2_AP_SYN          4
		#define RF2_AP_UPDATA       5
		#define RF2_AP_ACK          6

		#define RF_S_EVENT         10
		#define RF_S_STAT          11
		#define RF_S_UPDATA_ACK    12
		#define RF_S_EVENT_SIMPLE  13

		uint8_t cmd:4;
		#define CMD_NONE            0
		#define CMD_SET_SLOT         1
		#define CMD_ADJUST           2
		#define CMD_SET_UPDATA_MODE  3
		#define CMD_CLEAR_BAND_ID    4
		#define CMD_SET_MODE         5
	};
	uint8_t packet_seq;
}struct_rf_head;



typedef struct _rf_syn
{
	struct_rf_head head;
	uint8_t jump_ch_group;
}struct_rf_syn;


typedef struct _rf_updata
{
	struct_rf_head head;
	uint16_t addr;
	#define UPDATA_SENSOR_PACKET_LEN 16	
	uint8_t data[UPDATA_SENSOR_PACKET_LEN];
}struct_rf_updata;



typedef struct _rf_ack
{
	struct_rf_head head;
	uint8_t jump_ch_group;
	uint16_t sensor_id;
	uint8_t slot;
	uint8_t ack_bit[7];
}struct_rf_ack;


typedef union _SNP_EVENT_t
{
	uint16_t		uiAll;
	struct
	{
		uint16_t	bmMs	:	10,		// ???, 'SNP_REF_TIME_US'?32?: 0.9765625ms.
				bmSec	:	5,		// ??.
				blIsOn	:	1;		// ??'ON'/'OFF', 'ON' = 1.
		#define E_ON 1
		#define E_OFF 0
	};
} SNP_EVENT_t;

typedef struct _rf_event
{
	struct_rf_head head;
	uint8_t slot;
	uint8_t resend_count;
#ifdef SENSOR_EVENT_HAS_BACKRSSI
	int8_t background_rssi;
#endif
	SNP_EVENT_t event[1]; //可变长度 10 12 14 
}struct_rf_event;

typedef struct _rf_stat
{
	struct_rf_head head;
	uint8_t slot;
	uint16_t band_id;
	uint8_t battery;
	int8_t rx_rssi;
	union{
		uint16_t addr;
		struct{
		uint8_t h_version;
		uint8_t s_version;
		};
	};	
}struct_rf_stat;




typedef struct _sensor_list
{
	uint16_t sensor_id;
	struct _sensor_cfg
	{
		uint8_t mode;
		uint16_t on_delay;          //ON事件 延迟一段时间后输出，
		uint16_t off_to_on_min_time;  //分车阈值  表示OFF到ON的最小时间  如果小于这个时间 表示OFF是无效的
		uint8_t lane;      //车道号    从1开始
		uint8_t lane_index;  //车道里的序号   从0开始
		uint16_t distance; //距离前一个地磁的距离   毫米
		uint8_t direction; //东南西北 方向
		uint8_t lane_direction; //直行  左转 右转  掉头
	}sensor_cfg; //参数配置
	uint8_t slot;
	uint8_t enable_adjust;
	int8_t enable_updata;
	#define ENABLE_UPDATA 1
	#define UNDATA_ING    2
	uint16_t sensor_rev_addr;
	struct 
	{	
		struct 
		{
			uint8_t size;
			uint32_t rev_slot_count;
			uint8_t event_packet[16];  //存储两条事件包，每条最大4个事件
		}sensor_event[2];
		struct_rf_stat rf_stat_packet;		
	}sensor_data;	//数据包记录
	struct 
	{
		SNP_EVENT_t event[2]; //上一个事件（已经处理过）  和 未处理的事件（）
		uint32_t rev_slot_count[2];  //收到的时间
		uint8_t now_sensor_onoff;
		int32_t onoff_time_ms;
		#define S_ON 1
		#define S_OFF 2
		uint8_t now_event_need_handle; //是否有事件需要处理

		uint32_t car_count; //过车统计
		/* data */
	}event_calc;   //过车数据计算结果以及过程数据

	struct _timer_data
	{
		uint16_t packet_count;     //包总数
		uint16_t packet_lost_count;   //丢包数
		uint16_t packet_resend_count;  //重传数		
		uint8_t net_out_times; //掉网次数
		uint16_t net_out_time; //掉网时间 累积		
		
		uint16_t car_count; //过车统计
		uint16_t car_type_count[5]; //分车型过车统计		
		uint8_t on_times;    //占有率
		uint8_t car_head_distance; //车头时距
		uint8_t car_to_car_distance; //车间距
	}timer;
		
	struct 
	{
		int8_t rssi[2];
		int32_t rssi_add[2];
		int8_t snr[2];
		int32_t snr_add[2];		
		int32_t rssi_count[2];		/* data */
		int8_t rssi_avg[2];
		int8_t snr_avg[2];
		uint8_t last_resend_times; //记录一下当前sensor上报的重传次数
		uint16_t event_count; //实际收到的事件个数
		uint16_t lost_event_count; //通过包序号计算出丢了的事件个数
		uint16_t resend_count[32];    //0表示重传总数
	}sensor_stat;  //sensor 状态统计
	
	struct _sensor_cfg_rev
	{
		uint8_t h_version;
		uint8_t s_version;		
		int8_t rx_rssi;
		uint8_t battery;
		uint8_t mode;		
	}sensor_cfg_rev;
	int8_t need_print_on_off;
}struct_sensor_list;


typedef struct _sys_status
{
	uint32_t unix_time;
	double gps[2];


}struct_sys_status;


typedef struct _sensor_cfg_4g
{
	uint16_t sensor_id;
	uint8_t mode;
	uint16_t on_delay;          //ON事件 延迟一段时间后输出，
	uint16_t off_to_on_min_time;  //分车阈值  表示OFF到ON的最小时间  如果小于这个时间 表示OFF是无效的
	uint8_t lane;      //车道号    从1开始
	uint8_t lane_index;  //车道里的序号   从0开始
	uint16_t distance; //距离前一个地磁的距离   毫米
	uint8_t direction; //东南西北 方向
	uint8_t lane_direction; //直行  左转 右转  掉头

}struct_sensor_cfg_4g;


typedef struct _ap_param
{
	uint16_t crc;
	uint32_t code;  //标记码 统一为0xaa5555aa
	#define FLASH_CODE 0xaa5555aa
	uint32_t ap_id;
	uint16_t sensor_firmware_software_version;  //存放的sensor固件版本号	
	uint16_t io_firmware_software_version;  //存放的sensor固件版本号	
	uint16_t ap_version;  //ap固件版本号
	uint16_t on_delay;          //ON事件 延迟一段时间后输出，
	uint16_t off_to_on_min_time;  //分车阈值  表示OFF到ON的最小时间  如果小于这个时间 表示OFF是无效的
	struct
	{
		uint32_t ip;
		uint16_t port;
		struct 
		{
			uint8_t realtime_data_switch:1; //实时数据开关
			uint8_t timer_data_switch:1; //定时数据开关
		};
		uint16_t timer_data_time_sec; //定时数据间隔   秒
	}ip_port_param[3];
	uint8_t sensor_count;

}struct_ap_param;

typedef struct _sys_param
{
	struct_ap_param ap_param;
	struct_sensor_cfg_4g sensor_cfg_4g[1];
}struct_sys_param;


typedef struct _sensor_firmware_updata_manage
{
	uint32_t start_time;
	uint8_t resend_times;
	int8_t control_flag;
}struct_sensor_firmware_updata_manage;



unsigned short crc16(unsigned short crc, unsigned char const *buffer,int len);

void make_timer_data(void);
int32_t find_sensor_index(uint16_t sensor_id);
void sensor_updata_stat_manage(void);



#pragma pack()




extern RTC_TimeTypeDef rtc_time;
extern RTC_DateTypeDef rtc_date;

extern struct_sensor_firmware_updata_manage sensor_firmware_updata_manage;


#endif

